package com.iafme.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.StudyReqDTO;
import com.iafme.dto.res.StudyResDTO;
import com.iafme.model.Study;
import com.iafme.service.StudyService;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/1/20 18:38
 */
@RestController
@AllArgsConstructor
@RequestMapping(value = "/study_info")
public class StudyInfoController implements BaseCommonInterface<StudyReqDTO, StudyResDTO> {



    private final StudyService studyService;


    @OperateAnnotation(businessType = BusinessType.INSERT,name = "活动信息")
    @SaCheckPermission("activity:info:add")
    @Override
    public Result<String> save(@Valid StudyReqDTO studyReqDTO) {
        Study study = Convert.convert(Study.class, studyReqDTO);
        studyService.save(study);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "活动信息")
    @SaCheckPermission("activity:info:edit")
    @Override
    public Result<String> update(@Valid StudyReqDTO studyReqDTO) {
        Study study = Convert.convert(Study.class, studyReqDTO);
        studyService.updateById(study);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.DELETE,name = "活动信息")
    @SaCheckPermission("activity:info:delete")
    @Override
    public Result<String> del(Integer id) {
        studyService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<StudyResDTO>> lists(StudyReqDTO studyReqDTO) {
         return Result.successResult(studyService.lists(studyReqDTO));
    }

    @Override
    public Result<StudyResDTO> findById(Integer id) {
        Study study = studyService.getById(id);
        return Result.successResult(Convert.convert(StudyResDTO.class, study));
    }

    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "活动信息")
    @SaCheckPermission("activity:info:delete")
    @Override
    public Result<String> batchDel(List<StudyResDTO> list) {
        List<Integer> ids = list.stream().map(StudyResDTO::getId).collect(Collectors.toList());
        studyService.removeByIds(ids);
        return Result.successResult();
    }
}
